package com.fare.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.PlaneInfo;

/**
 * 二期通用类
 * @author spring.华 
 */
public class PriceUtil {

	/**
	 * 得到没有重复的flightNO
	 * @param infos
	 * @return
	 */
	public static Set<String> getFlightNo(List<FlightInfoVO> infos){
		Set<String> set =new HashSet<String> ();
		for (FlightInfoVO info : infos) {
			set.add(info.getFlightNo());
		}
		return set;
	}
	
	/**
	 * 得到没有重复的航班号
	 * @param infos
	 * @return
	 */
	/**
	 * 排重，航班号会重复出现
	 */
	public static List<FlightInfoVO> generateOnly(List<FlightInfoVO> vos){
		List<FlightInfoVO> finalResult=new ArrayList<FlightInfoVO>();
		Set<String> set=new HashSet<String>();
		for(FlightInfoVO vo :vos){
			if(set.add(vo.getFlightNo())==true){finalResult.add(vo);}
		}
		return finalResult;
	}
	
	
	/**
	 * 得到没有重复的flightNO
	 * @param infos
	 * @return
	 */
	public static Map<String,FlightInfoVO> getFlightNoMap(List<FlightInfoVO> infos){
		Map<String,FlightInfoVO> map =new HashMap<String,FlightInfoVO> ();
		for (FlightInfoVO info : infos) {
			map.put(info.getFlightNo(), info);
		}
		return map;
	}
	
	public static void main(String[] args) {
		Map map=new HashMap();
		map.put("2", "234");
		map.put("2", "eeee");
		System.out.println(map);
	}
	
	public static String getFlightNo(String flight){
		int len=flight.length();
		int addS=(8-len);
		StringBuffer sb=new StringBuffer("");
		for (int i = 0; i < addS; i++) {
			sb.append("0");
		}
		return sb.toString()+flight;
	}
	

	/**
	 * 比较出渠道的最低价
	 * @param dateList 时间集合 
	 * @param qudaoList
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<PlaneInfo>  getLowsPlaneInfo(List<String> dateList ,List<List<PlaneInfo>> qudaoList){
		///比较出渠道的最低价
		List<PlaneInfo> dataList=new ArrayList<PlaneInfo>();
		for(int i=0;i<dateList.size();i++){//天数
			String date=dateList.get(i);
			float lower=0f;
			PlaneInfo info=new PlaneInfo();
			for (Iterator iterator = qudaoList.iterator(); iterator.hasNext();) {
				List<PlaneInfo> infos = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = infos.iterator(); iterator2
						.hasNext();) {
					PlaneInfo pinfo = (PlaneInfo) iterator2.next();
					if(StringUtils.isEmpty(pinfo.getLowerPrice())){
						continue;
					}
					if(pinfo.getFlightBatchDate().equals(date)){
						if(lower==0f&&StringUtils.isNotEmpty(pinfo.getLowerPrice())){
							info=pinfo;
							lower=1f;
						}else if(StringUtils.isEmpty(pinfo.getLowerPrice())){
							continue;
						}
						else{//进行比较
							if(Float.parseFloat("".equals(info.getLowprPrice())? info.getLowerPrice():info.getLowprPrice())>Float.parseFloat("".equals(pinfo.getLowprPrice())? pinfo.getLowerPrice():pinfo.getLowprPrice())){
								info=pinfo;
							}
						}
						//break;
					}
				}
			}
			if(info!=null&&StringUtils.isNotEmpty(info.getLowerPrice())){
				dataList.add(info);
			}
		}	
		
		return dataList;
	}
	/**
	 * 根据不同的天数来比较出那天的最低价（专门为了提前售票运价分析服务的工具类）,然后按天数返回list
	 * 
	 * @param dateList
	 * @param qudaoList
	 * @return
	 */
	public static List<PlaneInfo>  getLowsPlaneInfoByDifferentDate(List<String> dates,Map<String,List<PlaneInfo>> map){
		List<PlaneInfo> finalResult=new ArrayList<PlaneInfo>();
		for(String date:dates){
			
			List<PlaneInfo> infos=map.get(date);//每天的所有数据
			Collections.sort(infos,new Comparator<PlaneInfo>() {

				@Override
				public int compare(PlaneInfo o1, PlaneInfo o2) {
					o1.setLowprPrice(o1.getLowerPrice()==null?999999+"":o1.getLowerPrice());
					o2.setLowprPrice(o2.getLowerPrice()==null?999999+"":o2.getLowerPrice());
					return Integer.parseInt(o1.getLowprPrice())-Integer.parseInt(o2.getLowprPrice());
				}
			});
			if(infos!=null && !infos.isEmpty()){
				finalResult.add(infos.get(0));//最低的数据
			}else{
				finalResult.add(new PlaneInfo());//
			}
		}
		
		return finalResult;
	}
	
	public static PlaneInfo getLowerest(List<PlaneInfo> infos){
		PlaneInfo finalResult=new PlaneInfo();
		Collections.sort(infos,new Comparator<PlaneInfo>() {

			@Override
			public int compare(PlaneInfo o1, PlaneInfo o2) {
				o1.setLowprPrice(o1.getLowerPrice()==null?999999+"":o1.getLowerPrice());
				o2.setLowprPrice(o2.getLowerPrice()==null?999999+"":o2.getLowerPrice());
				return Integer.parseInt(o1.getLowprPrice())-Integer.parseInt(o2.getLowprPrice());
			}
		});
		if(infos==null || infos.isEmpty()||infos.get(0).getLowprPrice().trim().equals(999999+"")){
			finalResult.setLowprPrice("0");
		}else{
			finalResult=infos.get(0);
		}
		return finalResult;
	}
	
	
	public static List<PlaneInfo>  getMyLowsPlaneInfo(List<String> dateList ,List<PlaneInfo> myList){
		///比较出渠道的最低价
		List<PlaneInfo> dataList=new ArrayList<PlaneInfo>();
		for(int i=0;i<dateList.size();i++){//天数
			String date=dateList.get(i);
			float lower=0f;
			PlaneInfo info=new PlaneInfo();
			//for (Iterator iterator = qudaoList.iterator(); iterator.hasNext();) {
				///List<PlaneInfo> infos = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = myList.iterator(); iterator2
						.hasNext();) {
					PlaneInfo pinfo = (PlaneInfo) iterator2.next();
					if(StringUtils.isEmpty(pinfo.getLowerPrice())){
						continue;
					}
					if(pinfo.getFlightBatchDate().equals(date)){
						if(lower==0f&&StringUtils.isNotEmpty(pinfo.getLowerPrice())){
							info=pinfo;
							lower=1f;
						}else if(StringUtils.isEmpty(pinfo.getLowerPrice())){
							continue;
						}
						else{//进行比较
							if(Float.parseFloat(info.getLowprPrice()+"")>Float.parseFloat(pinfo.getLowprPrice())){
								info=pinfo;
							}
						}
						////break;
					}
				}
		//	}
			if(info!=null&&StringUtils.isNotEmpty(info.getLowerPrice())){
				dataList.add(info);
			}
		}	
		
		return dataList;
	}
}
